package com.java.hibernate.dao.impl;

import com.java.hibernate.dao.SpringBootHibernateCommonDao;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @Author: Dgg
 * @Date: 2022/4/22 21:38
 */
public class SpringBootHibernateCommonDaoImpl<T> extends HibernateDaoSupport implements SpringBootHibernateCommonDao<T> {
	private boolean cacheQueries = false;

	private Class<?> entityClass = java.io.Serializable.class;

	@Override
	protected void initDao() throws Exception {
		super.initDao();
		getHibernateTemplate().setCacheQueries(cacheQueries);
	}

	@Resource(name = "clubSessionFactory")
	public void setSessionFacotry(SessionFactory sessionFacotry) {
		super.setSessionFactory(sessionFacotry);
	}

	public Session getSession() {
		return super.getSessionFactory().getCurrentSession();
	}

	@Override
	public void clearTemplate() {
		super.getHibernateTemplate().clear();
	}


	@Override
	public Serializable save(final T entity) {
		return getHibernateTemplate().save(entity);
	}

	@Override
	public void update(final T entity) {
		getHibernateTemplate().update(entity);
	}

	@Override
	public void delete(final T entity) {
		getHibernateTemplate().delete(entity);
	}

	@Override
	public void saveOrUpdate(final T entity) {
		getHibernateTemplate().saveOrUpdate(entity);
	}

	@Override
	@SuppressWarnings("unchecked")
	public T loadById(final Serializable id) {
		if (isFenBiao()) {
			throw new UnsupportedOperationException("分表不支持");
		} else {
			return (T) getHibernateTemplate().get(getEntityClass(), id);
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<T> loadListByProperty(String name, Object val, String sort) {
		if (isFenBiao()) {
			if (StringUtils.isNotBlank(name) && name.equals("superMerchantId")) {
				Criteria criteria = getSession().createCriteria(getEntityClass());
				criteria.add(Restrictions.eq(name, val));
				if (StringUtils.isNotEmpty(sort)) {
					criteria.addOrder(Order.desc(sort));
				}
				return criteria.list();
			} else {
				throw new UnsupportedOperationException("分表不支持");
			}
		} else {
			Criteria criteria = getSession().createCriteria(getEntityClass());
			criteria.add(Restrictions.eq(name, val));
			if (StringUtils.isNotEmpty(sort)) {
				criteria.addOrder(Order.desc(sort));
			}
			return criteria.list();
		}

	}

	@Override
	@SuppressWarnings("unchecked")
	public List<T> loadListByProperty(String name, Object val) {
		if (isFenBiao()) {
			if (StringUtils.isNotBlank(name) && name.equals("superMerchantId")) {
				Criteria criteria = getSession().createCriteria(getEntityClass());
				criteria.add(Restrictions.eq(name, val));
				return criteria.list();
			} else {
				throw new UnsupportedOperationException("分表不支持");
			}
		} else {
			Criteria criteria = getSession().createCriteria(getEntityClass());
			criteria.add(Restrictions.eq(name, val));
			return criteria.list();
		}

	}

	@Override
	public T loadUniqueByProperty(String name, Object val) {
		T t = null;
		List<T> list = loadListByProperty(name, val);
		if (list != null && !list.isEmpty()) {
			t = list.get(0);
		}
		return t;
	}

	protected Class<?> getEntityClass() {
		return entityClass;
	}

	protected void setEntityClass(Class<?> entityClass) {
		this.entityClass = entityClass;
	}

	protected String escapeSql(String str) {
		if (str.indexOf("'") >= 0)
			str = str.replaceAll("'", "\\\\'");
		if (str.indexOf("\"") >= 0)
			str = str.replaceAll("\"", "\\\\\"");
		return str;
	}

	@Override
	public void refreshSessionObject(T t) {
		getSession().refresh(t);
	}

	protected boolean isFenBiao() {
		return false;
	}

}
